//
//  AddressTableViewController.m
//  AXF
//
//  Created by Stone丶熊小叔 on 2017/1/4.
//  Copyright © 2017年 zw. All rights reserved.
//

#import "AddressTableViewController.h"
#import "AddressTableViewCell.h"
#import "AddressModel.h"
#import <Masonry.h>
#import "AddressDetailTableViewController.h"
@interface AddressTableViewController ()<addressTableViewCellDelegate>
@property (nonatomic,strong) NSArray * addArrData;
@property (nonatomic,weak)UILabel *name;
@property (nonatomic,weak)UILabel *phone;
@property (nonatomic,weak)UILabel *add;
@property (nonatomic,assign) NSInteger btnTag;
@end

static NSString *addCellID = @"addCellID";

@implementation AddressTableViewController

// 重写tableViewController的指定的初始化方法在里面改它的样式
- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}
//更改间隔
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [AddressModel getAddressModelWithSuccess:^(NSArray *arr) {
        self.addArrData = arr;
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"哎呦喂,又出错了%@",error);
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AddressTableViewCell" bundle:nil] forCellReuseIdentifier:addCellID];

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.addArrData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addCellID forIndexPath:indexPath];
    AddressModel *model = self.addArrData[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    cell.editBtn.tag = indexPath.row;
    self.btnTag = indexPath.row;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark 自定义cell的代理方法
-(void)jump2DetailController:(UIButton *)btn
{

    AddressDetailTableViewController *detailVC = [[AddressDetailTableViewController alloc] init];
    detailVC.addArrData = self.addArrData;
    detailVC.indexTag = btn.tag;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC returnModelData:^(NSArray *arrData) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.addArrData = arrData;
            
        }];
         [self.tableView reloadData];
    }];
   

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     AddressTableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
    self.name  = cell.nameLabel;
    self.phone = cell.phoneLabel;
    self.add = cell.addressLabel;
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
